library(tidyverse)
library(forcats)
library(dplyr)
library(foreign)
library(plotly)
library(survival)
library(flexsurv)  
library(aod)       
library(haven)
library(magrittr)
library(survminer)
library(lmtest)
library(sandwich)
library(stargazer)
library(MASS)
library(countrycode)
library(plm)
library(knitr)
library(pscl)


## load data 

psd = read.csv("psd_main_aggregated_v1.csv")

psd1 = dplyr::select(psd, cname_psd, ccode_cow, year, x_khansettlementype)

psd1 = rename(psd1, ccode = ccode_cow)


chioda = read.csv("chioda.csv")

chioda1 = subset(chioda, Recommended.For.Aggregates=="Yes")

## delete regional recipient 

chioda1 = subset(chioda1, Recipient!="Africa, regional"&Recipient!="Asia, regional"&Recipient!="Europe, regional"&Recipient!="America, regional"&Recipient!="Middle East, regional"&Recipient!="Oceania, regional")
chioda1 = subset(chioda1, Recipient.Region!="Multi-Region")

## Calculate total number of projects and the dollar amount of flow

## total flow and count: both ODA and OOF

chioda.total.count = chioda1 %>%
  group_by(Recipient, Commitment.Year) %>%
  summarise(count=n())

## total volume: both ODA and OOF

chioda1$amount = as.numeric(gsub(",","",chioda1$Amount..Constant.USD.2021.))

chioda.total.volume = chioda1[!is.na(chioda1$amount), ]

chioda.total.volume1 = aggregate(chioda.total.volume$amount, by=list(chioda.total.volume$Recipient,
                                                                     chioda.total.volume$Commitment.Year), 
                                 FUN=sum, na.rm=TRUE)





## ODA
chioda.oda = subset(chioda1, Flow.Class=="ODA-like")

chioda.oda.count = chioda.oda %>%
  group_by(Recipient, Commitment.Year) %>%
  summarise(count=n())

chioda.oda1 = chioda.oda[!is.na(chioda.oda$amount), ]

chioda.oda.volume = aggregate(chioda.oda1$amount, by=list(chioda.oda1$Recipient,chioda.oda1$Commitment.Year), FUN=sum, na.rm=TRUE)

## OOF

chioda.oof = subset(chioda1, Flow.Class=="OOF-like"|Flow.Class=="Vague (Official Finance)")


chioda.oof.count = chioda.oof %>%
  group_by(Recipient, Commitment.Year) %>%
  summarise(count=n())

chioda.oof1 = chioda.oof[!is.na(chioda.oof$amount), ]

chioda.oof.volume = aggregate(chioda.oof1$amount, by=list(chioda.oof1$Recipient,chioda.oof1$Commitment.Year), 
                              FUN=sum, na.rm=TRUE)

## change variable names for chioda data

chioda.oda.count["ccode"] = countrycode(chioda.oda.count$Recipient, origin = "country.name", destination = "cown")
chioda.oda.count = mutate(chioda.oda.count, ccode=replace(ccode, Recipient=="Serbia", 345))
chioda.oda.count = rename(chioda.oda.count, year=Commitment.Year, oda.count=count)
chioda.oda.count = ungroup(chioda.oda.count)
chioda.oda.count = dplyr::select(chioda.oda.count, -Recipient)

chioda.oof.count["ccode"] = countrycode(chioda.oof.count$Recipient, origin = "country.name", destination = "cown")
chioda.oof.count = mutate(chioda.oof.count, ccode=replace(ccode, Recipient=="Serbia", 345))
chioda.oof.count = rename(chioda.oof.count, year=Commitment.Year, oof.count=count)
chioda.oof.count = ungroup(chioda.oof.count)
chioda.oof.count = dplyr::select(chioda.oof.count, -Recipient)

chioda.oda.volume["ccode"] = countrycode(chioda.oda.volume$Group.1, origin = "country.name", destination = "cown")
chioda.oda.volume = mutate(chioda.oda.volume, ccode=replace(ccode, Group.1=="Serbia", 345))
chioda.oda.volume = rename(chioda.oda.volume, year=Group.2, oda.volume=x)
chioda.oda.volume = dplyr::select(chioda.oda.volume, -Group.1)


chioda.oof.volume["ccode"] = countrycode(chioda.oof.volume$Group.1, origin = "country.name", destination = "cown")
chioda.oof.volume = mutate(chioda.oof.volume, ccode=replace(ccode, Group.1=="Serbia", 345))
chioda.oof.volume = rename(chioda.oof.volume, year=Group.2, oof.volume=x)
chioda.oof.volume = dplyr::select(chioda.oof.volume, -Group.1)

chioda.total.count["ccode"] = countrycode(chioda.total.count$Recipient, origin = "country.name", destination = "cown")
chioda.total.count = mutate(chioda.total.count, ccode=replace(ccode, Recipient=="Serbia", 345))
chioda.total.count = rename(chioda.total.count, year=Commitment.Year, total.count=count)
chioda.total.count = ungroup(chioda.total.count)
chioda.total.count = dplyr::select(chioda.total.count, -Recipient)

chioda.total.volume1["ccode"] = countrycode(chioda.total.volume1$Group.1, origin = "country.name", destination = "cown")
chioda.total.volume1 = mutate(chioda.total.volume1, ccode=replace(ccode, Group.1=="Serbia", 345))
chioda.total.volume1 = rename(chioda.total.volume1, year=Group.2, total.volume=x)
chioda.total.volume1 = dplyr::select(chioda.total.volume1, -Group.1)

psd1 = left_join(psd1, chioda.oda.count)
psd1 = left_join(psd1, chioda.oda.volume)
psd1 = left_join(psd1, chioda.oof.volume)
psd1 = left_join(psd1, chioda.oof.count)
psd1 = left_join(psd1, chioda.total.count)
psd1 = left_join(psd1, chioda.total.volume1)

psd1$oda.count[is.na(psd1$oda.count)] = 0
psd1$oof.count[is.na(psd1$oof.count)] = 0
psd1$total.count[is.na(psd1$total.count)] = 0
psd1$oda.volume[is.na(psd1$oda.volume)] = 0
psd1$oof.volume[is.na(psd1$oof.volume)] = 0
psd1$total.volume[is.na(psd1$total.volume)] = 0

psd1$log.oda.volume = log(psd1$oda.volume+1)
psd1$log.oof.volume = log(psd1$oof.volume+1)
psd1$log.total.volume = log(psd1$total.volume+1)

psd2 = subset(psd1, year>=2000)


## add region

gtd = read_csv("gtd.csv")
gtd = dplyr::select(gtd, country_txt, region)
gtd$ccode = countrycode(gtd$country_txt, origin = "country.name", destination = "cown")
gtd = mutate(gtd, ccode=replace(ccode, country_txt=="Serbia", 345)) 

gtd = dplyr::select(gtd, -country_txt)
gtd = unique(gtd)

psd2 = left_join(psd2,gtd)

psd3 = subset(psd2, region==11)

model = lm(log.oda.volume~ x_esidettlementype+log_gdppc+log_pop+log_attack+gdp_growth1+
             log_trade+log_resource+v2x_polyarchy+Civil_War+factor(ccode), 
           data = psd3)

model = lm(log.oda.volume~ x_esidettlementype+factor(ccode), 
           data = psd3)

model = lm(log.oof.volume~ x_esidettlementype+log_gdppc+log_pop+log_attack+gdp_growth1+
             log_trade+log_resource+v2x_polyarchy+Civil_War+factor(ccode), 
           data = psd3)

model = lm(log.oof.volume~ x_esidettlementype+factor(ccode), 
           data = psd3)

model = glm.nb(oof.count~ x_esidettlementype+log_gdppc+log_pop+log_attack+gdp_growth1+
             log_trade+log_resource+v2x_polyarchy+factor(ccode), 
           data = psd3)
coeftest(model, vcov. = vcovCL(model, cluster = ~ccode, type = "HC0"))

model = glm.nb(oda.count~ x_esidettlementype+log_gdppc+log_pop+log_attack+gdp_growth1+
                 log_trade+log_resource+v2x_polyarchy+factor(ccode), 
               data = psd3)
coeftest(model, vcov. = vcovCL(model, cluster = ~ccode, type = "HC0"))

psd3 = left_join(psd3, psd1)

psd3 = read_csv("psd_china.csv")

write.csv(psd3, file = "psd_china.csv")

westedu = dplyr::select(westedu, ccode,year, Civil_War)

psd3 = left_join(psd3, westedu)

summary(model)


model = lm(log.oda.volume~ x_khansettlementype+log_gdppc+log_pop+log_attack+gdp_growth1+
             log_trade+log_resource+v2x_polyarchy+factor(ccode), 
           data = psd3)
coeftest(model, vcov. = vcovCL(model, cluster = ~ccode, type = "HC0"))



model = lm(log.oda.volume~ x_esidettlementype+factor(ccode), 
           data = psd3)

model = lm(log.oof.volume~ x_khansettlementype+log_gdppc+log_pop+log_attack+gdp_growth1+
             log_trade+log_resource+v2x_polyarchy+factor(ccode), 
           data = psd3)


psd3$x_esidettlementype1 = psd3$x_esidettlementype
psd3$x_esidettlementype1[is.na(psd3$x_esidettlementype1)] = 'not_settled'
psd3$x_esidettlementype1 = as.factor(psd3$x_esidettlementype1)

psd3 <- psd3 %>%
  mutate(x_esidettlementype1 = relevel(x_esidettlementype1, ref = "3. Narrow-Disp."))

model = lm(log.oda.volume~ x_esidettlementype1+log_gdppc+log_pop+log_attack+gdp_growth1+
             log_trade+log_resource+v2x_polyarchy+factor(ccode), 
           data = psd3)
coeftest(model, vcov. = vcovCL(model, cluster = ~ccode, type = "HC0"))



model = lm(log.oda.volume~ x_esidettlementype+factor(ccode), 
           data = psd3)

model = lm(log.oof.volume~ x_esidettlementype1+log_gdppc+log_pop+log_attack+gdp_growth1+
             log_trade+log_resource+v2x_polyarchy+factor(ccode), 
           data = psd3)





+v2x_polyarchy
+log_attack+log_trade+gdp_growth1

psd3$x_esidettlementype = as.factor(psd3$x_esidettlementype)

psd3$x_esidettlementype[psd3$x_esidettlementype==''] = NA

psd3$x_khansettlementype = as.factor(psd3$x_khansettlementype)
psd3$x_khansettlementype[psd3$x_khansettlementype==''] = NA

psd3 <- psd3 %>%
  mutate(x_khansettlementype = relevel(x_khansettlementype, ref = "3. Narrow-Disp."))

psd3 <- psd3 %>%
  mutate(x_esidettlementype = relevel(x_esidettlementype, ref = "3. Narrow-Disp."))

psd3$x_khansettlementype = as.factor(psd3$x_khansettlementype)



leader.2000.11 = dplyr::select(leader.2000.2, ccode, year, 
                       log_trade)
psd = dplyr::select(psd, -log_trade)
psd = left_join(psd, leader.2000.11)
